(function(app) {
    app
        .controller("content_15_checkCtrl", ["$scope", "myFactory", '$http', '$stateParams', '$state', function($scope, myFactory, $http, $stateParams, $state) {

            //接收参数
            var item15 = $stateParams.item15;
            //var mid=$stateParams.mid;
            var itemids = $stateParams.itemids;
            var chdrnum01 = $stateParams.chdrnum;
            var state = $stateParams.state; //跳转路由
            var chdrcoy = $stateParams.chdrcoy;
            $scope.chdrnum01 = chdrnum01;
            //映射到页面
            $scope.item15 = $stateParams.item15;
            $scope.showFlag = true; //控制前端是否显示“详情、重审、退出”三个功能
            $scope.newBenefit = false;
            if ($scope.item15.benefit != null || $scope.item15.benefit != undefined) {
            	if($scope.item15.benefit.length>0){
                	$scope.newBenefit = true;
                }
            }
            /*S2022013021184监管类-银行令〔2022〕1号营运相关系统功能需求 add by huanghaokun 20220315 start*/
            $scope.reasonShow = false;//是否显示减保原因
            if($scope.item15.reason!=null){
            	 $scope.reasonShow = true;
            }
    		/*S2022013021184监管类-银行令〔2022〕1号营运相关系统功能需求 add by huanghaokun 20220315 end*/
            
            $scope.data = {
                    changeCon: []
                },
                //重新受理
                $scope.renew = function() {
                    myFactory.renew();
                }
                //退出移动保全
            $scope.exit = function() {
                    myFactory.exit();
                }
                //返回函数
            $scope.goBack = function() {
                //history.go(-1);
                //				$state.go(state,{
                //					"item":item,
                //					"chdrnum01":chdrnum01,
                //					"mid":mid,
                //					"itemids":itemids
                //				});
                myFactory.goBack();
            }

            //保单详情弹窗
            $scope.showPolicyDetail = function() {
                modal('open', 'policyModalOn15Check', 'policyWindowOn15Check', 'policyCloseXOn15Check');
                var token = sessionStorage.getItem("token");
                var timestamp = myFactory.getTimetamp();
                var sfis = "chdrnum-chdrcoy-token-timestamp";
                var signStr = "chdrnum=" + chdrnum01 + "&chdrcoy=" + chdrcoy + "&token=" + token + "&timestamp=" + timestamp; //encodeURI(name)
                var sign = myFactory.hex(signStr);
                //初始化查询订单详情
                var policyData;
                $http({
                    method: 'GET',
                    url: app.domain + 'query/restFindPolicyDetail.action',
                    params: { 'chdrnum': chdrnum01, 'chdrcoy': chdrcoy, 'sign': sign, 'sfis': sfis, 'token': token, 'timestamp': timestamp }
                }).then(function successCallback(response) {
                    if (response.data.success) {
                        policyData = response.data.data;
                        $scope.policyData = policyData;
                    }
                }, function errorCallback(response) {
                    $.alert({
                        title: '',
                        text: '初始化查询订单详情失败!',
                        onOK: function() {
                            //点击确认
                        }
                    });
                });

            }

            //保单详情弹窗关闭函数
            $scope.policyCancelOn15Check = function() {
                modal('close', 'policyModalOn15Check', 'policyWindowOn15Check', 'policyCloseXOn15Check');
            }

            //关闭遮罩
            $scope.overwindowClose = function() {
                //$(".overwindow").show();
                $(".overwindow").hide();
                $(".overmodal").hide();
            }

            //根据路由判断是否要显示详情、重新受理、退出三个功能
            if ("history_detail" == state) {
                $scope.showFlag = false; //不显示
            }
            $scope.changeBenefit = function(index) {
                $.showLoading('正在处理,请稍候');
                setTimeout(function() {
                    $.hideLoading();
                }, 300);

                console.log(index);
                modal('open', 'editPeopleModal_check', 'overwindowOn15Input', 'editPeopleModalCloseX_check');
                /**
                 * 数据加载
                 */
                var item = $scope.item15.benefit[index];

                $("#benfTypeEdit").val(item.benfType);
                $("#editNo").val(index); //表明当前操作的元素是第几个

                $("#xzNameEdit").val(item.riskname);
                $("#beEdit").val(item.riskamount);
                $("#qbfEdit").val(item.riskpremium);

                //加载是否投连/万能险
                if (item.typeRisk == "0") {
                    $scope.EditPopSexFlag = false;
                    $scope.eqjshow = true;
                } else {
                    $scope.EditPopSexFlag = true;
                    $scope.eqjshow = false;
                }

            }
            $scope.editPeopleCancel = function() {

                modal('close', 'editPeopleModal_check', 'overwindowOn15Input', 'editPeopleModalCloseX_check');
            }

            $scope.editPeopleComfirm = function() {
                /**
                 * 获取数据
                 */
                var name = $("#xzNameEdit").val(); //名字
                var be = $("#beEdit").val();
                var qbf = $("#qbfEdit").val();
                var typeRisk;
                if ($scope.EditPopSexFlag == false) {
                    typeRisk = "0";
                } else {
                    typeRisk = "1";
                }

                if (typeRisk == "0") {
                    if (qbf == null || qbf.trim() == "") {
                        myFactory.alert("", "期缴保险费不能为空", "info");
                        return;
                    }
                }
                var item = {
                    "riskname": name,
                    "riskamount": be,
                    "riskpremium": qbf,
                    "typeRisk": typeRisk
                }
                var index = $("#editNo").val(); //得到当前操作的元素是第几个

                $scope.item15.benefit[index] = item; //将所操作的对象数据进行调整
                save();
                modal('close', 'editPeopleModal_check', 'overwindowOn15Input', 'editPeopleModalCloseX_check'); //关闭弹框
            }
            $scope.deletePeople = function() {
                if ($scope.item15.benefit.length == 0) {
                    return;
                }
                $.confirm("是否删除该附加险？", "", function() {
                    var afterDetele = [];
                    $("#check15People tbody tr").each(function(index, el) { //index为数据条数，从0开始，el代表整行
                        if ($(el).find("input.weui-check").prop("checked") == true) {
                            //$(el).remove();//页面移除该行,不使用该行，会出现JQ跟anglar冲突的问题，采用让repeat标签自动根据item08.benefit更新就行了
                            $scope.item15.benefit[index] = ""; //置空对应index函数
                        }
                    });

                    for (var i = 0; i < $scope.item15.benefit.length; i++) {
                        if ($scope.item15.benefit[i] != "") {
                            afterDetele.push($scope.item15.benefit[i]); //如果不为空，放入处理后数组
                        }
                    }
                    $scope.item15.benefit = afterDetele; //最后将数组替换item08.benefit即可，最后数据提交也提交这个即可
                    save();
                });
            }

            function save() {
                //进行保存到后台
            	myFactory.loadingShow();//显示加载圈
    			var changeItem15_increment = sessionStorage.getItem("changeItem15_increment");
    			var token=sessionStorage.getItem("token");
    			var timestamp=myFactory.getTimetamp();
    			var sfis= "token-timestamp";
    			var signStr="token="+token+"&timestamp="+timestamp;//encodeURI(name)
    			var sign = myFactory.hex(signStr);
    			var params15Map={
    					"itemId":itemId,
    					"itemStr":$scope.item15,
    					"changeItem15_increment":changeItem15_increment
    					};
    			$http({
    				method: 'post',
    				url: app.domain + 'input/saveContentItem15.action',
    				data:{paramsMap:params15Map,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
    			}).then(function(result) {

                }, function(result) {
                    myFactory.alert('', '连接服务器超时，请稍后重试！', 'info');
                });
    			/*var sfis= "itemId-token-timestamp";
    			var signStr="itemId="+"15"+"&token="+token+"&timestamp="+timestamp;
    			var sign = myFactory.hex(signStr);
                $scope.readFlag = false;
    			$http({
    				method: 'GET',
    				url: app.domain + 'input/saveContentItem15.action',
    				params: {'itemId': itemId,'itemStr':$scope.item15,'changeItem15_increment' : changeItem15_increment,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
    			}).then(function(result) {

                }, function(result) {
                    myFactory.alert('', '连接服务器超时，请稍后重试！', 'info');
                });*/

            }
        }]);

    //指针弹窗
    app.directive('infoid', function() {
        return {
            restrict: 'EA', //指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
            templateUrl: 'html/input/content_base_info.html' + app.clearCache,
            replace: true, //设置解析后，DOM声明的指令是否消失
            link: function(scope, element, attrs) { //链接函数负责注册DOM事件和更新DOM,
                console.log('指令完成');
            }
        }
    });

})(app)